package com.duing.handler;

import com.duing.bean.GraphBean;
import com.duing.util.HttpURLConnectUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GraphHandler {

    //获取动态得数据
    private static String urlStr = "https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList,nowConfirmStatis,provinceCompare";

    public static List<GraphBean> getGraphData(){

        List<GraphBean> graphBeans = new ArrayList<>();

        //获取到数据
        String str = HttpURLConnectUtil.doGet(urlStr);
        //利用gson 解析数据
        Gson gson = new Gson();
        Map map = gson.fromJson(str, Map.class);
//        System.out.println(map);

        Map dataMap = (Map)map.get("data");
        ArrayList dayAddList = (ArrayList)dataMap.get("chinaDayAddList");
//        System.out.println(dayAddList);

        for (int i = 0; i < dayAddList.size(); i++) {
            //获取每次遍历得数据
            Map tmp = (Map)dayAddList.get(i);
            String data = (String)tmp.get("date");
            double confirm = (double)tmp.get("confirm");
            double suspect = (double)tmp.get("suspect");

            GraphBean graphBean = new GraphBean(data,(int)confirm,(int)suspect);
            graphBeans.add(graphBean);
        }

        return graphBeans;
    }

    public static void main(String[] args) {
        getGraphData();
    }
}
